package com.nd.service.wx;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.FootprintMapper;
import com.nd.dao.GoodsMapper;
import com.nd.pojo.bean.*;
import com.nd.pojo.vo.wx.FootprintDataVo;
import com.nd.pojo.vo.wx.FootprintListVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class FootprintServiceImpl implements FootprintService{

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    GoodsMapper goodsMapper;



    @Override
    public FootprintDataVo list(Integer page, Integer size) {

        PageHelper.startPage(page, size);

        // 用户ID查取goodsID
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Integer userId = user.getId();

        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        criteria.andUserIdEqualTo(userId);

        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);

        Collections.sort(footprints, new Comparator<Footprint>() {
            @Override
            public int compare(Footprint o1, Footprint o2) {
                return o2.getUpdateTime().compareTo(o1.getUpdateTime());
            }
        });

        List<FootprintListVo> footprintListVos = new ArrayList<>();



        for (Footprint footprint : footprints) {

            Integer goodsId = footprint.getGoodsId();

            GoodsExample goodsExample = new GoodsExample();
            GoodsExample.Criteria criteria1 = goodsExample.createCriteria();
            criteria1.andIdEqualTo(goodsId);

            // 用ID拿到的goods是一个
            List<Goods> goods = goodsMapper.selectByExample(goodsExample);
            Goods goods1 = goods.get(0);
            FootprintListVo footprintListVo = new FootprintListVo();

            footprintListVo.setBrief(goods1.getBrief());
            footprintListVo.setPicUrl(goods1.getPicUrl());
            footprintListVo.setAddTime(footprint.getUpdateTime());
            footprintListVo.setGoodsId(footprint.getGoodsId());
            footprintListVo.setName(goods1.getName());
            footprintListVo.setId(footprint.getId());
            footprintListVo.setRetailPrice(goods1.getRetailPrice());

            footprintListVos.add(footprintListVo);

        }


        // Collections.reverse(footprintListVos);

        PageInfo<FootprintListVo> userPageInfo = new PageInfo<>(footprintListVos);
        long total = userPageInfo.getTotal();

        FootprintDataVo footprintDataVo = new FootprintDataVo();
        footprintDataVo.setFootprintList(footprintListVos);
        footprintDataVo.setTotalPages(total);
        //couponDataVo.setCount((int)total);

        return footprintDataVo;
    }

    @Override
    public Integer delete(Integer id) {

        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();

        criteria.andIdEqualTo(id);

        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);

        footprints.get(0).setDeleted(true);

        return 0;
    }
}
